package com.ht.medicaltreatment.activity.haocai2

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.SubjectListAdapter
import com.ht.medicaltreatment.bean.SubjectListDataPage
import com.ht.medicaltreatment.viewmodel.ConsumableViewModel
import kotlinx.android.synthetic.main.activity_subject_list_layout.btnPositive
import kotlinx.android.synthetic.main.activity_subject_list_layout.etFormName
import kotlinx.android.synthetic.main.activity_subject_list_layout.recyclerView
import kotlinx.android.synthetic.main.activity_subject_list_layout.tvTotal

//专题列表
class SubjectListActivity:BaseActivity(), OnItemClickListener {

    override fun getLayoutId() = R.layout.activity_subject_list_layout

    private val modle by viewModels<ConsumableViewModel>()
    private var page = 1
    private var protocolNumber = ""
    private lateinit var adapter:SubjectListAdapter

    override fun init() {
        setTitle("专题列表")
        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = SubjectListAdapter()
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
        //模糊查询
        btnPositive.setOnClickListener {
            protocolNumber = etFormName.text.toString()
            initData()
        }
    }

    override fun onResume() {
        super.onResume()
        initData()
    }

    private fun initData() {
        showProgress()
        modle.subjectList(page,protocolNumber,{
            dismissProgress()
            adapter.setNewInstance(it.pageList)
            tvTotal.text = "总数：${it.totalNumber}"
        })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as SubjectListDataPage
        val bundle = Intent(this,ConsumableListActivity::class.java)
        bundle.putExtra("id",item.id)
        startActivity(bundle)
    }
}
